var map;
var marker;
var index;
var poly;
var contentString;
var infowindow;
var vehicleid;
var prev_lat;
var prev_long;
var prev_time;
var distance = 0;
var num_stops = 0;

var vehiclepath = [ new google.maps.LatLng(12.095277, 76.555077),
		new google.maps.LatLng(12.095280, 76.555177),
		new google.maps.LatLng(12.095283, 76.555277),
		new google.maps.LatLng(12.095283, 76.555377) ];

function getSelectedVehicle() {
	var e = document.getElementById("vechicleid_dropdwn");
	var selectedvehicleID = e.options[e.selectedIndex].value;
	return selectedvehicleID;
}

function initialize() {
	index = 0;
	var mapOptions = {
		zoom : 18,
		center : vehiclepath[index],
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	vehicleid = getSelectedVehicle();
	alert(vehicleid);
	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	contentString = '<div id="content">' + '<p>' + vehicleid + '</p>' + '</div>';

	infowindow = new google.maps.InfoWindow({
		content : contentString
	});

	$.getJSON('ajax/GetVehiclePosition.action?vehicleID=' + vehicleid,
			function(data) {
				console.log(data.time);
				var lat = data.latitude;
				prev_lat = data.latitude;
				var lon = data.longitude;
				prev_long = data.longitude;
				var time = data.time;
				prev_time = new Date(data.time);
				
				var newPoint = new google.maps.LatLng(lat, lon);

				marker = addMarker(newPoint);
				infowindow.open(map, marker);
			});

	var polyOptions = {
		strokeColor : '#000000',
		strokeOpacity : 1.0,
		strokeWeight : 3
	};

	poly = new google.maps.Polyline(polyOptions);
	poly.setMap(map);

	setTimeout(trackvehicle, 3000);

}

function CalcDistanceBetween(lat1, lon1, lat2, lon2) {
    //Radius of the earth in:  1.609344 miles,  6371 km  | var R = (6371 / 1.609344);
    var R = 3958.7558657440545; // Radius of earth in Miles 
    var dLat = toRad(lat2-lat1);
    var dLon = toRad(lon2-lon1); 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
            Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    return d;
}

function toRad(Value) {
    /** Converts numeric degrees to radians */
    return Value * Math.PI / 180;
}

function addMarker(location) {
	marker = new google.maps.Marker({
		position : location,
		map : map,
		animation: google.maps.Animation.DROP
	});
	return marker;
}

function moveMarker(location) {
	marker.setPosition(location);
	map.panTo(location);
}

function getTimeInHours(t1, t2) {
	var Epoch_t1 = t1.getTime()/1000.0;
	var Epoch_t2 = t2.getTime()/1000.0;
	var diff = Epoch_t1 - Epoch_t2;
	return diff/60;
	
}

function trackvehicle() {
	index = index + 1;
	console.log(index);
	var path = poly.getPath();

	$.getJSON('ajax/GetVehiclePosition.action?vehicleID=' + vehicleid,
			function(data) {
				console.log(data.time);
				console.log(data.latitude);
				console.log(data.longitude);
				var lat = data.latitude;
				var lon = data.longitude;
				var time = data.time;
				var newPoint = new google.maps.LatLng(lat, lon);

				var short_distance = CalcDistanceBetween(lat, lon, prev_lat, prev_long);
				console.log('short_distance: ' + short_distance);
				distance = distance + short_distance;
				console.log('total distance: ' + distance);
				$('#distance').html('<p>' + 'Total distance travelled in miles: ' + distance + '</p>');
				
				short_interval = getTimeInHours(new Date(time), prev_time);
				console.log('short_interval: ' + short_interval);
				var speed = short_distance / short_interval;
				$('#speed').html('<p>' + 'Instantaneous speed: ' + speed + '</p>');
				if(speed == 0) {
					num_stops = num_stops + 1;
					$('#status').html('<p>' + 'Status: ' + 'vehicle stopped' + '</p>' + '<p>' + 'Number of stops: ' + num_stops + '</p>');
					
				}
				else {
					$('#status').html('<p>' + 'Status: ' + 'vehicle in motion' + '</p>' + '<p>' + 'Number of stops: ' + num_stops + '</p>');
				}
				
				prev_lat = lat;
				prev_long = lon;
				prev_time = new Date(time);
				
				
				var marker = addMarker(newPoint);
				infowindow.content = '<p>' + time + '</p>';
				infowindow.open(map, marker);
				path.push(newPoint);
				map.panTo(newPoint);
			});

	setTimeout(trackvehicle, 3000);

}
